<?php

	global $wpdb;
	require_once("trackclass.php");
	$objTrack = new trackClass();
	
	require_once("userclass.php");
	$objUser = new userClass();
	
	//$table_name =  'wp_spec_order_trac';
	$pagename   =  ' Order';
	
	$addme=$_POST["addme"];
	

	$act=$_REQUEST["act"];
	if($act=="upd")
	{
		$recid=$_REQUEST["id"];
		$sSQL="SELECT  wp_spec_order_trac.id as id,
							wp_spec_order_trac.order_id,
							wp_spec_order_trac.track_status,
							wp_spec_order_trac.assigned_user,	
							wp_spec_checkout.user_id	
			   			FROM wp_spec_order_trac
						INNER JOIN wp_spec_checkout ON wp_spec_order_trac.order_id = wp_spec_checkout.id
						WHERE wp_spec_order_trac.order_id=$recid";
						
						
		$result = mysql_query($sSQL) or die ('Error, query failed');
		if (mysql_num_rows($result) > 0 )
		{
			if($row = mysql_fetch_assoc($result))
			{
				$id        				= $row['id'];
				$order_id				= $row['order_id'];
				$track_status			= $row['track_status'];
				$track_message     		= $row['track_message'];
				$user_id				= $row['user_id'];
				$assigned_user          = $row['assigned_user'];
				//$btn	  			    = "Update".$pagename;
				$btn					= 'Assign Project';
				$hidval	  				= 2;
			}
		}
	}
	else
	{
		//$btn	     ="Add New".$pagename;
		$btn					= 'Assign Project';
		$id          = "";
		$name  	     = "";
		$description = "";
		$terms       = "";
		$sort_order  = "";
		$hidval	     = 1;

	}
	
	
?>
<div xmlns="http://www.w3.org/1999/xhtml" class="wrap nosubsub">

	<div class="icon32" id="icon-edit"><br/></div>


<?php 
$assigned_count =  $objTrack->get_assigned_count($order_id);
$page_type      =  $_REQUEST['page_type'];
if($page_type == 'reassign') {

	 $designer_id    = $objUser->getAssigned($order_id,$usertype='designer');
	 $photogrpher_id = $objUser->getAssigned($order_id,$usertype='photographer');


?>
<h2>Reassign the Photographer or Designer&nbsp;</h2>
<div id="col-center" style="width:900px;">
	<div class="col-wrap">

		<div>
			<div class="form-wrap">
				<h3></h3>
                <form class="validate" action="admin.php" enctype="multipart/form-data" method="post" id="addtag">
				
				<input type="hidden" name="order_id_reassign" value="<?php  echo $order_id;?>">
				<input type="hidden" name="old_designer" value="<?php  echo $designer_id;?>">
				<input type="hidden" name="old_photographer" value="<?php  echo $photogrpher_id;?>">
				
                <table width="100%" height="175">
                   <tr><td width="168" height="30"><strong>Order ID</strong></td><td width="622"><strong>#<?php echo $order_id;?></strong></td>
               
                
                <tr><td width="30%"><strong>Select the Photographer </strong></td><td> 
 								 <select name="reassgned_photographer" id="reassgned_designer">
                           		
                                    <?php
									
									echo $objUser->listAllUsers($photogrpher_id,'',$type='photographer');
									?>
                  </select> 
				 
                  </td>
				  
				  
				  </tr>
				   <tr><td>	   <strong>Select the Designer  </strong></td><td>
				  <?php 
				  # GET DESIGNER OF THE PROJECT 
				  /*$query		  = "SELECT assigned_to FROM wp_spec_assigned_orders WHERE order_id=".$order_id." AND assigned_to!=".$assigned_user." ";
				  $query_designer = mysql_query($query);
				  $row_designer   = mysql_fetch_object($query_designer );
				  $designer_id  = $row_designer->assigned_to;*/
				  ?>
			 <select name="reassgned_designer" id="reassgned_designer">
                           			
                                    <?php
									echo $objUser->listAllUsers($designer_id,'',$type='designer');
									?>
                  </select> 
                  </td></tr>
				  
				  
				  
               

                <tr>
                <td></td><td></td></tr>
                </table>
			  <p class="submit">
<input type="submit" value="Reassign" onClick="return validate();" class="button button-primary" id="submit" name="reassign_project"/>
						<input type="hidden" name="id" value="<?php echo $id;?>" >
                        <input type="hidden" name="order_id" value="<?php echo $order_id;?>" >
                        <input type="hidden" name="user_id" value="<?php echo $user_id;?>" />
                        <input type="hidden" name="type" value="Service" />
                       	<input type="hidden" name="addme" value="<?php echo $hidval;?>" >
					</p>
			  </form>
			</div>
		</div>
	</div>
</div>
</div>

<?php } 
else 
if($assigned_count==3){ ?>
<h2>Update order status&nbsp;</h2>
<div id="col-center" style="width:900px;">
	<div class="col-wrap">

		<div>
			<div class="form-wrap">
				<h3></h3>
                <form class="validate" action="admin.php" enctype="multipart/form-data" method="post" id="addtag">
                <table width="802" height="175">
                   <tr><td width="168" height="30"><strong>Order ID</strong></td><td width="622"><strong>#<?php echo $order_id;?></strong></td>
                <tr><td width="168"><strong>Work Status</strong></td><td width="622">
                 <select name="track_status" id="order_track_status" onchange="setMessage(this.val);">
                           
                           <?php echo  $objTrack->getTrackSelect($track_status);?>
                           </select></td></tr>
                
                <tr><td><strong>Assign to </strong></td><td> 
 								 <select name="assigned_user">
                           			<option value="0">User List</option>
                                    <?php
									echo $objUser->listAllUsers($assigned_user,$order_id,$type='');
									?>
                  </select> 
				  
				  <?php 
				  /*
				  
				  # GET DESIGNER OF THE PROJECT 
				  $query		  = "SELECT assigned_to FROM wp_spec_assigned_orders WHERE order_id=".$order_id." AND assigned_to!=".$assigned_user." ";
				  $query_designer = mysql_query($query);
				  $row_designer   = mysql_fetch_object($query_designer );
				  $designer_id  = $row_designer->assigned_to;
				  ?>
				  Assign to Designer <select name="project_designer">
                           			<option value="0">User List</option>
                                    <?php
									echo $objUser->listAllUsers($designer_id,$order_id,$type='designer');
									?>
                  </select> 
				  
				  <?php */ ?>
                  </td></tr>
                <tr>
                  <td><strong>Message to Customer</strong></td><td> <?php
					   		$settings = array( 'media_buttons' => false,'textarea_rows' => 7 );
							$content = $track_message;
							$editor_id = 'track_message';
							wp_editor( $content, $editor_id,$settings);
							?></td></tr>
                <tr>
                <tr>
                  <td><strong>Message to Resource</strong><br />(Designer/Photographer)</td><td> <?php
					   		$settings = array( 'media_buttons' => false,'textarea_rows' => 7 );
							$content = $message_for_assigned;
							$editor_id = 'message_for_assigned';
							wp_editor( $content, $editor_id,$settings);
							?></td></tr>
                <tr>
                <td></td><td></td></tr>
                </table>
			  <p class="submit">
<input type="submit" value="<?php echo $btn; ?>" class="button button-primary" id="submit" name="submit_track"/>
						<input type="hidden" name="id" value="<?php echo $id;?>" >
                        <input type="hidden" name="order_id" value="<?php echo $order_id;?>" >
                        <input type="hidden" name="user_id" value="<?php echo $user_id;?>" />
                        <input type="hidden" name="type" value="Service" />
                       	<input type="hidden" name="addme" value="<?php echo $hidval;?>" >
					</p>
			  </form>
			</div>
		</div>
	</div>
</div>
</div>
 
<?php } else {?>
<h2>Select a photographer and Designer&nbsp;</h2>
<div id="col-center" style="width:900px;">
	<div class="col-wrap">

		<div>
			<div class="form-wrap">
				<h3></h3>
                <form class="validate" action="admin.php" enctype="multipart/form-data" method="post" id="addtag">
                <table width="100%" height="175">
                   <tr><td width="168" height="30"><strong>Order ID</strong></td><td width="622"><strong>#<?php echo $order_id;?></strong></td>
               
                
                <tr><td width="30%"><strong>Select the Photographer </strong></td><td> 
 								 <select name="assigned_user" id="assigned_user">
                           			<option value="0">Photographers List</option>
                                    <?php
									echo $objUser->listAllUsers($assigned_user,'',$type='photographer');
									?>
                  </select> 
				 
                  </td>
				  
				  
				  </tr>
				   <tr><td>	   <strong>Select the Designer  </strong></td><td>
				  <?php 
				  # GET DESIGNER OF THE PROJECT 
				  $query		  = "SELECT assigned_to FROM wp_spec_assigned_orders WHERE order_id=".$order_id." AND assigned_to!=".$assigned_user." ";
				  $query_designer = mysql_query($query);
				  $row_designer   = mysql_fetch_object($query_designer );
				  $designer_id  = $row_designer->assigned_to;
				  ?>
			 <select name="project_designer" id="project_designer">
                           			<option value="0">Designers List</option>
                                    <?php
									echo $objUser->listAllUsers($designer_id,'',$type='designer');
									?>
                  </select> 
                  </td></tr>
				  
				  
				  
               

                <tr>
                <td></td><td></td></tr>
                </table>
			  <p class="submit">
<input type="submit" value="<?php echo $btn; ?>" onClick="return validate();" class="button button-primary" id="submit" name="assign_project"/>
						<input type="hidden" name="id" value="<?php echo $id;?>" >
                        <input type="hidden" name="order_id" value="<?php echo $order_id;?>" >
                        <input type="hidden" name="user_id" value="<?php echo $user_id;?>" />
                        <input type="hidden" name="type" value="Service" />
                       	<input type="hidden" name="addme" value="<?php echo $hidval;?>" >
					</p>
			  </form>
			</div>
		</div>
	</div>
</div>
</div>

<?php } ?>
  
<script>
function validate(){
	var photographer = jQuery( "#assigned_user" ).val();
	if(photographer == '0'){
		alert('Please select the Photographer');
		jQuery('#assigned_user').focus();
		return false;
	}
	var designer = jQuery( "#project_designer" ).val();
	if(designer == '0'){
		alert('Please select the Designer');
		jQuery(project_designer).focus();
		return false;
	}
}
function setMessage(val){

	if((jQuery('#order_track_status').val())==0){
		 jQuery('#track_message').val("We will be contacting you within 24 hours to schedule your photo shoot.");
	}
	if((jQuery('#order_track_status').val())==1){
		 jQuery('#track_message').val("We will be contacting you within 24 hours to schedule your photo shoot.");
	}
	if((jQuery('#order_track_status').val())==2){
		 jQuery('#track_message').val("Date and Time.");
	}
	if((jQuery('#order_track_status').val())==3){
		 jQuery('#track_message').val("Link to pictures for designer.");
	}
	if((jQuery('#order_track_status').val())==4){
		 jQuery('#track_message').val("Your photos are being edited.");
	}
	if((jQuery('#order_track_status').val())==5){
		 jQuery('#track_message').val("Your tour has been completed. Please download the images and view the tour here.");
	}
	
	
	}
</script>